<!--
 * @Description: 企业微信第三方登录
 * @Version: 1.0
 * @Author: Yuanjy
 * @Date: 2020-03-09 20:41:16
 * @LastEditors: Yuanjy
 * @LastEditTime: 2020-05-06 11:30:30
 -->
<template>
  <div>
    <div v-if="code" class="el-loading-mask">
      <div class="el-loading-spinner" style="top:30%">
        <svg viewBox="25 25 50 50" class="circular" style="width:62px;height:62px;">
          <circle cx="50" cy="50" r="20" fill="none" class="path" />
        </svg>
        <p class="el-loading-text" style="margin:20px 0; font-size:16px;">登录中，请稍候......</p>
      </div>
    </div>
  </div>
</template>

<script>
import { getQueryString } from '@/utils'
import { setToken } from '@/utils/auth'

export default {
  data() {
    return {
      code: getQueryString('auth_code'),
      corpid: getQueryString('appid'),
    }
  },
  created() {
    if (this.code) {
      this.wwxThirdLogin()
    } else {
      this.$router.push('/login?redirect=/dashboard')
    }
  },
  methods: {
    wwxThirdLogin() {
      if (this.code == null || !this.code) {
        this.$router.push('/login?redirect=/dashboard')
        return
      }
      this.$api.permission.auth
        .wwxThirdLogin({
          data: { code: this.code, corpid: this.corpid }
        })
        .then(res => {
          setToken(res.data.token)
          this.$router.push('/dashboard')
        })
        .catch(error => {
          this.$router.push('/login?redirect=/dashboard')
          console.log(error)
        })
    }
  }
}
</script>
